<?php
/**
 * 模板消息设置
 */
namespace Api;
use Kuyuan\WqCore\api\Api;
use Kuyuan\WqCore\util\Util;
use Kuyuan\WqCore\wx\Message;

class SettingMessage extends Api
{
    private $Setting = null;

    public function __construct()
    {
        parent::__construct();
        $this->Setting = new \Controller\SettingMessage();
    }

    /**
     * 获取参数
     */
    public function find()
    {
        Util::appointGet();
        $_setting = $this->Setting->find();
        Util::returns(array("code"=>0,"data"=>$_setting));
    }

    /**
     * 设置参数
     */
    public function set()
    {
        Util::appointPost();
        if($this->Setting->set($this->_gpc["data"])){
            $_result = array("code"=>0);
        } else {
            $_result = array("code"=>103);
        }
        Util::returns($_result);
    }

    /**
     * 一键配置模板消息id
     */
    public function autoSetTplMessage()
    {
        Util::appointPost();
        // 获取模板消息所属行业&&判断当前公众号是否设置了“IT科技/IT软件与服务”
        $_tpl_class = Message::getTplClass();
        $_class = array();
        foreach($_tpl_class as $_v) {
            $_class[] = implode("/",$_v);
        }
        try {
            if(!in_array("IT科技/IT软件与服务",$_class)) {
                throw new \Exception("未添加所属行业“IT科技/IT软件与服务”",1000);
            }

            // 获取当前设置的模板消息ID,删除当前设置的模板消息id
            $_message_data = $this->Setting->find();
            if($_message_data["await_check_tpl"]) {
                Message::delTplId($_message_data["await_check_tpl"]);
            }
            if($_message_data["result_check_tpl"]) {
                Message::delTplId($_message_data["result_check_tpl"]);
            }
            // 待审核提醒 模板编号 OPENTM418073234
            // 审核结果通知  模板编号 OPENTM417949050
            // 获取模板消息id
            $_setting_data = array(
                "await_check_tpl" => Message::getTplId("OPENTM418073234"),
                "result_check_tpl" => Message::getTplId("OPENTM417949050")
            );
            if(!$this->Setting->set($_setting_data)) {
                throw new \Exception("一键配置失败",103);
            }
            Util::returns(array("code"=>0,"data"=>$_setting_data));
        } catch (\Exception $e) {
            Util::returns(array("code"=>$e->getCode(),"err_msg"=>$e->getMessage()));
        }
    }

    /**
     * 一键配置订阅消息id
     */
    public function autoSetSubscribeMessage()
    {
        Util::appointPost();
        try {
            $_subscribe_class = Message::getSubscribeClass();
            if(!$_subscribe_class) {
                throw new \Exception("一键配置失败",1000);
            }
            if (!in_array("413",array_column((array)$_subscribe_class,"id"))) {
                throw new \Exception("请先在公众号内的订阅通知添加服务类目“IT科技>软件服务提供商”",1000);
            }
            $_message_data = $this->Setting->find();
            if($_message_data["result_check_sub"]) {
                Message::delSubscribeTpl($_message_data["result_check_sub"]);
            }
            $_tpl_id = Message::addSubscribeTemplate(
                "5545",
                array(10,8,4,9),
                "通知用户上传素材审核状态");
            if(!$_tpl_id) {
                throw new \Exception("配置失败",1000);
            }
            $_setting_data = array(
                "result_check_sub" => $_tpl_id
            );
            if(!$this->Setting->set($_setting_data)) {
                throw new \Exception("一键配置失败",103);
            }
            Util::returns(array("code"=>0,"data"=>$_tpl_id));
        } catch (\Exception $e) {
            Util::returns(array("code"=>$e->getCode(),"err_msg"=>$e->getMessage()));
        }
    }

}
